
#include "../ui.h"
#include "../app_includes.h"

#define DEBUG_TAB_NUM      (1)

static uint8_t tab_id = 0;
static lv_obj_t *ui_tabview;
static lv_obj_t *ui_tab[DEBUG_TAB_NUM];
static lv_timer_t *lv_timer;
static void debug_group_select(uint8_t id);
static void debug_tab0_screen_create(lv_obj_t *tab);
static void debug_tab1_screen_create(lv_obj_t *tab);

static void debug_tab0_refresh(lv_timer_t *timer);

void lv_debug_close(void)
{
    uint16_t angle[] = {0, 90, 180, 270};
    lcd_154hsc04a_rotation(&board.lcd_dev, angle[ui_main_screen.dis_dir]);
    lv_status_bar_hidden(0);
    lv_timer_del(lv_timer);
}

//app应用入口
lv_obj_t *lv_debug_open(lv_obj_t *screen)
{
    lcd_154hsc04a_rotation(&board.lcd_dev, 90);
    ui_tabview = lv_tab_screen_create(screen, ui_tab, DEBUG_TAB_NUM);
    debug_tab0_screen_create(ui_tab[0]);
    if (DEBUG_TAB_NUM > 1) {
        debug_tab1_screen_create(ui_tab[1]);
    }
    debug_group_select(0);
    lv_status_bar_hidden(1);
    
    lv_timer = lv_timer_create(debug_tab0_refresh, 1000, NULL);
    
    cdc_rx_num = 0;
    cdc_tx_num = 0;
    //xEventGroupSetBits(sys_event, kSysEvent_UIDaplinkChange);
    return ui_tabview;
}

static lv_obj_t * label_add(lv_obj_t *panel, char  *name, lv_coord_t y, uint32_t color)
{
    lv_obj_t *label = lv_label_create(panel);
    lv_obj_set_size(label, 50, LV_SIZE_CONTENT);
    lv_obj_align( label, LV_ALIGN_CENTER, 0, y );
    lv_label_set_text(label, name);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(label, lv_color_hex(color), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    return label;
}

static lv_obj_t *ui_labelIDCode;
static lv_obj_t *ui_labelDaplinkSpeed;
static lv_obj_t *ui_labelUartConfig;
static lv_obj_t *ui_labelUartDataNum;
static void debug_tab0_screen_create(lv_obj_t *tab)
{
    lv_obj_t *panel, *label;
    panel = lv_obj_create(tab);
    lv_obj_set_width( panel, 70);
    lv_obj_set_height( panel, 220);
    lv_obj_align( panel, LV_ALIGN_CENTER, 80 ,0 );
    lv_obj_clear_flag( panel, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(panel, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(panel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_side(panel, LV_BORDER_SIDE_LEFT, LV_PART_MAIN| LV_STATE_DEFAULT);

    label_add(panel, "  VCC  ", -20, 0xC86464);
    label_add(panel, "  DIO  ", -40, 0x2095F6);
    label_add(panel, "  CLK  ", -60, 0x2F4F2F);
    label_add(panel, "  GND  ", -80, 0x292831);

    label_add(panel, "  VCC  ", 80, 0xC86464);
    label_add(panel, "  RXD  ", 60, 0x2095F6);
    label_add(panel, "  TXD  ", 40, 0x2F4F2F);
    label_add(panel, "  GND  ", 20, 0x292831);

    label = lv_label_create(tab);
    lv_obj_set_width( label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( label, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( label, LV_ALIGN_TOP_LEFT, 0, -10 );
    lv_label_set_text(label,"  DAPLINK  ");
    lv_obj_set_style_text_font(label, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(label, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(label, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    label = lv_label_create(tab);
    lv_obj_set_width( label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( label, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( label, LV_ALIGN_TOP_LEFT, 0, 15 );
    lv_label_set_text(label,"IDCODE");
    lv_obj_set_style_text_color(label, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_labelIDCode = lv_label_create(tab);
    lv_obj_set_width( ui_labelIDCode, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_labelIDCode, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_labelIDCode, label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_labelIDCode,"00000000");
    lv_obj_set_style_text_font(ui_labelIDCode, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    label = lv_label_create(tab);
    lv_obj_set_width( label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( label, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( label, ui_labelIDCode, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_label_set_text(label,"SWD");
    lv_obj_set_style_text_color(label, lv_color_hex(0x60A3E7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_labelDaplinkSpeed = lv_label_create(tab);
    lv_obj_set_width( ui_labelDaplinkSpeed, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_labelDaplinkSpeed, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_labelDaplinkSpeed, label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_labelDaplinkSpeed,"5MHz");
    lv_obj_set_style_text_font(ui_labelDaplinkSpeed, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);

    label = lv_label_create(tab);
    lv_obj_set_width( label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( label, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( label, ui_labelDaplinkSpeed, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_label_set_text(label,"  UART         ");
    lv_obj_set_style_text_font(label, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(label, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(label, lv_color_hex(0x2095F6), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    label = lv_label_create(tab);
    lv_obj_set_width( label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( label, LV_SIZE_CONTENT);   /// 1
    lv_obj_align( label, LV_ALIGN_TOP_LEFT, 0, 130 );
    lv_label_set_text(label,"CONFIG");
    lv_obj_set_style_text_color(label, lv_color_hex(0xC86464), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_labelUartConfig = lv_label_create(tab);
    lv_obj_set_width( ui_labelUartConfig, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_labelUartConfig, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_labelUartConfig, label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_labelUartConfig,"9600 8 N 1");
    lv_obj_set_style_text_font(ui_labelUartConfig, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);

    label = lv_label_create(tab);
    lv_obj_set_width( label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( label, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( label, ui_labelUartConfig, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_label_set_text(label,"R / T");
    lv_obj_set_style_text_color(label, lv_color_hex(0x60A3E7), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(label, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_labelUartDataNum = lv_label_create(tab);
    lv_obj_set_width( ui_labelUartDataNum, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_labelUartDataNum, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_labelUartDataNum, label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(ui_labelUartDataNum,"0B/0B");
    lv_obj_set_style_text_font(ui_labelUartDataNum, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
}

extern const char *stop_name[];
extern const char *parity_name[];
extern uint8_t swd_read_idcode(uint32_t *id);
static void debug_tab0_refresh(lv_timer_t *timer)
{
    EventBits_t event;
    char str[32];
    uint16_t speed = 0;
    uint32_t id = 0;
    //event = xEventGroupWaitBits(sys_event, kSysEvent_UIDaplinkChange, pdTRUE, pdFALSE, 0);  
    //if(event & kSysEvent_UIDaplinkChange) { //有更新事件
        if(DAP_Data.clock_delay>0) {
            speed = 32000/DAP_Data.clock_delay;
            if(speed>=1000)
                uint_to_str(str, speed/1000, 0, 0, "MHz");
            else
                uint_to_str(str, speed, 0, 0, "KHz");
            lv_label_set_text(ui_labelDaplinkSpeed, str);
        }    
        if (swd_read_idcode(&id)) {
            hex_arr_to_str_swap(str, (uint8_t *)&id, 4);
            lv_label_set_text(ui_labelIDCode, str);
        }
        
        if(usart1_state.g_cdc_lincoding.dwDTERate==0) {
            strcpy(str, "NOT OPEN");
        }
        else {
            uint_to_str(str, usart1_state.g_cdc_lincoding.dwDTERate, 0, 0, " ");
            uint_to_str(str+strlen(str), usart1_state.g_cdc_lincoding.bDataBits, 0, 0, parity_name[usart1_state.g_cdc_lincoding.bParityType]);
            strcat(str, stop_name[usart1_state.g_cdc_lincoding.bCharFormat]);
        }
        lv_label_set_text(ui_labelUartConfig, str);
        if(cdc_rx_num<65535)
            uint_to_str(str, cdc_rx_num, 0, 0,  "B/");
        else
            uint_to_str(str, cdc_rx_num/1024, 0, 0,  "KB/");
        if(cdc_tx_num<65535)
            uint_to_str(str+strlen(str), cdc_tx_num, 0, 0,  "B");
        else
            uint_to_str(str+strlen(str), cdc_tx_num/1024, 0, 0,  "KB");
        lv_label_set_text(ui_labelUartDataNum, str);
        
    //}

    
}

static void debug_tab1_screen_create(lv_obj_t *tab)
{

}


static void debug_group_select(uint8_t id)
{

}
